package executes;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * This does not do so much at the moment. It takes the specified xml file. Looks for the tags "Placemark"
 * Loops over all the nodes longitude and writes them in a text file. I use it to create the xls sheet Mountains4000
 * At the moment the results are written to txt and this should be written to xls
 * From kml file create xls and from xls create a package
 * Package --> markers --> context --> messages --> message properties --> explore files --> utils --> oracle
 * Do it complete or does some files have to be updated like utils and suggestbox in passes?
 */
public class ExtractKMLfile {

	public static void main(String argv[]) {

		try {
			File file = new File("c:\\KML\\mountains4000.xml");
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(file);
			doc.getDocumentElement().normalize();
			System.out.println("Root element " + doc.getDocumentElement().getNodeName());
			NodeList nodeLst = doc.getElementsByTagName("Placemark");
			System.out.println("Longitude Latitude and Name of mountain");

			FileWriter fstream = new FileWriter("C:\\KML\\mountain.txt");
			BufferedWriter textFile = new BufferedWriter(fstream);
			for (int s = 0; s < nodeLst.getLength(); s++) {

				Node fstNode = nodeLst.item(s);

				if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

					Element fstElmnt = (Element) fstNode;
					NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("latitude");
					Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
					NodeList fstNm = fstNmElmnt.getChildNodes();
					String mountainName = ((Node) fstNm.item(0)).getNodeValue();
					textFile.write(mountainName + "\n");
					System.out.println("Latitude : " + ((Node) fstNm.item(0)).getNodeValue());
					
					NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("longitude");
					Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
					NodeList lstNm = lstNmElmnt.getChildNodes();
					System.out.println("Longitude : " + ((Node) lstNm.item(0)).getNodeValue());
				}
				
			}
			textFile.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}